package org.ksystem.practise.url;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class GetUrlFiles {

    private String HTTP_URL = "http://localhost/";

    private String FILE_01 = HTTP_URL + "01.jpg";

    /**
     * 获取URL的指定的资源
     * 
     * @throws IOException
     */
    public void getUrlResource() throws IOException {
        URL url = new URL(HTTP_URL);
        Object object = url.getContent();
        System.out.println(object.toString());
    }

    /**
     * 获取URL指定的资源
     * 
     * @throws IOException
     */
    public void getUrlResourceByInputstream() throws IOException {
        URL url = new URL(HTTP_URL);
        // 返回一个 URLConnection 对象，它表示到 URL 所引用的远程对象的连接。
        URLConnection connection = url.openConnection();
        // 打开的连接读取的输入流。
        InputStream in = connection.getInputStream();
        int c;
        while ((c = in.read()) != -1) {
            System.out.print(c);
        }
        in.close();
    }

}
